﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Text.RegularExpressions;

    [ScriptType("SP.RoleDefinitionCollection", ServerTypeId="{964b9ab0-d026-4487-99d1-e06450963cc9}")]
    public class RoleDefinitionCollection : ClientObjectCollection<RoleDefinition>
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public RoleDefinitionCollection(ClientRuntimeContext Context, ObjectPath ObjectPath) : base(Context, ObjectPath)
        {
        }

        [Remote]
        public RoleDefinition Add(RoleDefinitionCreationInformation parameters)
        {
            if (base.Context.ValidateOnClient)
            {
                if (parameters == null)
                {
                    throw ClientUtility.CreateArgumentNullException("parameters");
                }
                if (parameters != null)
                {
                    if (parameters.Name == null)
                    {
                        throw ClientUtility.CreateArgumentNullException("parameters.Name");
                    }
                    if ((parameters.Name != null) && (parameters.Name.Length == 0))
                    {
                        throw ClientUtility.CreateArgumentException("parameters.Name");
                    }
                    if ((parameters.Name != null) && (parameters.Name.Length > 0xff))
                    {
                        throw ClientUtility.CreateArgumentException("parameters.Name");
                    }
                    if ((parameters.Name != null) && !Regex.Match(parameters.Name, @"^[^\[\]/\\:\|<>\+=;,\?\*'@]*$").Success)
                    {
                        throw ClientUtility.CreateArgumentException("parameters.Name");
                    }
                    if ((parameters.Description != null) && (parameters.Description.Length > 0x200))
                    {
                        throw ClientUtility.CreateArgumentException("parameters.Description");
                    }
                    if (parameters.BasePermissions == null)
                    {
                        throw ClientUtility.CreateArgumentNullException("parameters.BasePermissions");
                    }
                }
            }
            RoleDefinition definition = new RoleDefinition(base.Context, new ObjectPathMethod(base.Context, base.Path, "Add", new object[] { parameters }));
            definition.Path.SetPendingReplace();
            ObjectIdentityQuery query = new ObjectIdentityQuery(definition.Path);
            base.Context.AddQueryIdAndResultObject(query.Id, definition);
            base.Context.AddQuery(query);
            base.AddChild(definition);
            definition.InitFromCreationInformation(parameters);
            return definition;
        }

        [Remote]
        public RoleDefinition GetById(int id)
        {
            Dictionary<int, RoleDefinition> dictionary;
            object obj2;
            if (base.ObjectData.MethodReturnObjects.TryGetValue("GetById", out obj2))
            {
                dictionary = (Dictionary<int, RoleDefinition>) obj2;
            }
            else
            {
                dictionary = new Dictionary<int, RoleDefinition>();
                base.ObjectData.MethodReturnObjects["GetById"] = dictionary;
            }
            RoleDefinition definition = null;
            if (!dictionary.TryGetValue(id, out definition))
            {
                definition = new RoleDefinition(base.Context, new ObjectPathMethod(base.Context, base.Path, "GetById", new object[] { id }));
                dictionary[id] = definition;
                ObjectIdentityQuery query = new ObjectIdentityQuery(definition.Path);
                base.Context.AddQueryIdAndResultObject(query.Id, definition);
                base.Context.AddQuery(query);
            }
            return definition;
        }

        [Remote]
        public RoleDefinition GetByName(string name)
        {
            Dictionary<string, RoleDefinition> dictionary;
            object obj2;
            if (base.Context.ValidateOnClient)
            {
                if (name == null)
                {
                    throw ClientUtility.CreateArgumentNullException("name");
                }
                if ((name != null) && (name.Length == 0))
                {
                    throw ClientUtility.CreateArgumentException("name");
                }
                if ((name != null) && (name.Length > 0xff))
                {
                    throw ClientUtility.CreateArgumentException("name");
                }
            }
            if (base.ObjectData.MethodReturnObjects.TryGetValue("GetByName", out obj2))
            {
                dictionary = (Dictionary<string, RoleDefinition>) obj2;
            }
            else
            {
                dictionary = new Dictionary<string, RoleDefinition>();
                base.ObjectData.MethodReturnObjects["GetByName"] = dictionary;
            }
            RoleDefinition definition = null;
            if (!dictionary.TryGetValue(name, out definition))
            {
                definition = new RoleDefinition(base.Context, new ObjectPathMethod(base.Context, base.Path, "GetByName", new object[] { name }));
                dictionary[name] = definition;
                ObjectIdentityQuery query = new ObjectIdentityQuery(definition.Path);
                base.Context.AddQueryIdAndResultObject(query.Id, definition);
                base.Context.AddQuery(query);
            }
            return definition;
        }

        [Remote]
        public RoleDefinition GetByType(RoleType roleType)
        {
            Dictionary<RoleType, RoleDefinition> dictionary;
            object obj2;
            if (base.ObjectData.MethodReturnObjects.TryGetValue("GetByType", out obj2))
            {
                dictionary = (Dictionary<RoleType, RoleDefinition>) obj2;
            }
            else
            {
                dictionary = new Dictionary<RoleType, RoleDefinition>();
                base.ObjectData.MethodReturnObjects["GetByType"] = dictionary;
            }
            RoleDefinition definition = null;
            if (!dictionary.TryGetValue(roleType, out definition))
            {
                definition = new RoleDefinition(base.Context, new ObjectPathMethod(base.Context, base.Path, "GetByType", new object[] { roleType }));
                dictionary[roleType] = definition;
                ObjectIdentityQuery query = new ObjectIdentityQuery(definition.Path);
                base.Context.AddQueryIdAndResultObject(query.Id, definition);
                base.Context.AddQuery(query);
            }
            return definition;
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            bool flag = base.InitOnePropertyFromJson(peekedName, reader);
            if (flag)
            {
                return flag;
            }
            return flag;
        }
    }
}

